import io
import ddddocr
from PIL import Image,ImageDraw,ImageFont
from fontTools.ttLib import TTFont
# ocr = ddddocr.DdddOcr(show_ad=False)


def make_font_maps(file):
    ocr = ddddocr.DdddOcr(show_ad=False)

    font_maps = {}
    font = TTFont(file)
    img_font = ImageFont.truetype(file, size=24)
    for code,name in font.getBestCmap().items():
        # print(chr(code),name)
        if code == 120:
            continue
        else:
            img = Image.new("RGB", (35, 35), (255, 255, 255))

            draw = ImageDraw.Draw(img)
            draw.text((0,0), chr(code), (0, 0, 0), font=img_font)
            # img.save(f"./static/font/img/{code}.png", "png")

            img_bytes = io.BytesIO()
            img.save(img_bytes, "png")
            img_value = img_bytes.getvalue()

            r = ocr.classification(img_value)

            # print(code,r)
            font_maps[hex(code)] = r

    return font_maps




if __name__ == '__main__':
    # result = make_font_maps("../static/font/file")
    result = make_font_maps("../static/font/fa-solid-900.woff2")

    print(result)